﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Xyz.Web.Reservation.AddonService;
using Xyz.Web.Reservation.AircraftSeatService;
using Xyz.Web.Reservation.BookingService;
using Xyz.Web.Reservation.CustomerService;
using Xyz.Web.Reservation.PassengerService;
using FlightDTO = Xyz.Web.Reservation.BookingService.FlightDTO;
using PassengerDTO = Xyz.Web.Reservation.BookingService.PassengerDTO;

namespace Xyz.Web.Reservation.Models
{
    public static class Mapper
    {
        #region Booking

        public static BookingDTO FromModel(this BookingModel obj)
        {
            return new BookingDTO
            {
                AdultNo = obj.AdultNo,
                BookingId = obj.BookingId,
                BookingCode = obj.BookingCode,
                ChildNo = obj.ChildNo,
                ContactEmailAddress = obj.ContactEmailAddress,
                ContactName = obj.ContactName,
                ContactNo = obj.ContactNo,
                PaymentDue = obj.PaymentDue,
                Status = obj.Status,
                TotalTicketFare = obj.TotalTicketFare,
                TripCode = obj.TripCode,
                BookingDate = obj.BookingDate,
                Tickets = FromModel(obj.Tickets),
                CustomerId = obj.CustomerId
            };
        }

        public static List<BookingDTO> FromModel(this List<BookingModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToList();
        }

        public static BookingModel ToModel(this BookingDTO obj)
        {
            if (obj == null)
                return null;

            return new BookingModel
                       {
                           AdultNo = obj.AdultNo,
                           BookingCode = obj.BookingCode,
                           BookingId = obj.BookingId,
                           ChildNo = obj.ChildNo,
                           ContactEmailAddress = obj.ContactEmailAddress,
                           ContactName = obj.ContactName,
                           ContactNo = obj.ContactNo,
                           PaymentDue = obj.PaymentDue,
                           Status = obj.Status,
                           TotalTicketFare = obj.TotalTicketFare,
                           TripCode = obj.TripCode,
                           BookingDate = obj.BookingDate,
                           Tickets = ToModel(obj.Tickets),
                           CustomerId = obj.CustomerId
                       };
        }

        public static List<BookingModel>  ToModel(this List<BookingDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }
        #endregion

        #region Ticket
        public static TicketModel ToModel(this TicketDTO obj)
        {
            return new TicketModel
            {
                BookingId = obj.BookingId,
                Flight = ToModel(obj.Flight),
                FlightId = obj.FlightId,
                FlightSeat = ToModel(obj.FlightSeat),
                Passenger = ToModel(obj.Passenger),
                Services = ToModel(obj.Services),
                TicketFare = obj.TicketFare
            };
        }

        public static List<TicketModel> ToModel(this TicketDTO[] obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        public static TicketDTO FromModel(this TicketModel obj)
        {
            return new TicketDTO
            {
                BookingId = obj.BookingId,
                Flight = FromModel(obj.Flight),
                FlightId = obj.FlightId,
                FlightSeat = FromModel(obj.FlightSeat),
                Passenger = FromModel(obj.Passenger),
                Services = FromModel(obj.Services),
                TicketFare = obj.TicketFare
            };
        }
        public static TicketDTO[] FromModel(this List<TicketModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToArray();
        }

        #endregion
		
        #region SeatModel
        public static SeatModel ToModel(this AircraftSeatDTO obj)
        {
			if (obj == null)
            {
                return null;
            }
            return new SeatModel
            {
                AircraftId = obj.AircraftId,
                AircraftSeatId = obj.AircraftSeatId,
                IsAisleSeat = obj.IsAisleSeat,
                IsWindowSeat = obj.IsWindowSeat,
                RewardPoint = obj.RewardPoint,
                SeatColumn = obj.SeatColumn,
                SeatNo = obj.SeatNo,
                SeatRow = obj.SeatRow,
                SeatTypeCode = obj.SeatTypeCode
            };
        }
		  public static List<SeatModel> ToModel(this List<AircraftSeatDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }
		
        #endregion
		
        #region FlightSeatModel
        public static FlightSeatModel ToModel(this FlightSeatDTO obj)
        {
            return new FlightSeatModel
            {
                FlightSeatId = obj.FlightSeatId,
                Status = obj.Status,
                AircraftSeatId = obj.AircraftSeatId,
                FlightId = obj.FlightId
                //AircraftId = obj.AircraftId,
                //AircraftSeatId = obj.AircraftSeatId,
                //IsAisleSeat = obj.IsAisleSeat,
                //IsWindowSeat = obj.IsWindowSeat,
                //RewardPoint = obj.RewardPoint,
                //SeatColumn = obj.SeatColumn,
                //SeatNo = obj.SeatNo,
                //SeatRow = obj.SeatRow,
                //SeatTypeCode = obj.SeatTypeCode
            };
        }

        public static FlightSeatDTO FromModel(this FlightSeatModel obj)
        {
            return new FlightSeatDTO
            {
                FlightSeatId = obj.FlightSeatId,
                Status = obj.Status,
                AircraftSeatId = obj.AircraftSeatId,
                FlightId = obj.FlightId
                //AircraftId = obj.AircraftId,
                //AircraftSeatId = obj.AircraftSeatId,
                //IsAisleSeat = obj.IsAisleSeat,
                //IsWindowSeat = obj.IsWindowSeat,
                //RewardPoint = obj.RewardPoint,
                //SeatColumn = obj.SeatColumn,
                //SeatNo = obj.SeatNo,
                //SeatRow = obj.SeatRow,
                //SeatTypeCode = obj.SeatTypeCode
            };
        }

        #endregion

        #region PassengerModel
        public static PassengerModel ToModel(this PassengerDTO obj)
        {
            return new PassengerModel
            {
                ContactNo = obj.ContactNo,
                DateOfBirth = obj.DateOfBirth,
                EmailAddress = obj.EmailAddress,
                FirstName = obj.FirstName,
                TitleCode = obj.TitleCode,
                LastName = obj.LastName,
                PassengerId = obj.PassengerId,
                PassportExpiryDate = obj.PassportExpiryDate,
                PassportNo = obj.PassportNo
            };
        }
        public static PassengerDTO FromModel(this PassengerModel obj)
        {
            return new PassengerDTO
            {
                ContactNo = obj.ContactNo,
                DateOfBirth = obj.DateOfBirth,
                EmailAddress = obj.EmailAddress,
                FirstName = obj.FirstName,
                TitleCode = obj.TitleCode,
                LastName = obj.LastName,
                PassengerId = obj.PassengerId,
                PassportExpiryDate = obj.PassportExpiryDate,
                PassportNo = obj.PassportNo
            };
        }

        public static List<PassengerDTO> FromModel(this List<PassengerModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToList();
        }
        #endregion

        #region ServiceModel
        internal static ServiceModel ToModel(this ServiceDTO obj)
        {
            if (obj == null)
            {
                return null;
            }
            return new ServiceModel
            {
                Price = obj.Price,
                ServiceCode = obj.ServiceCode,
                ServiceDescription = obj.ServiceDescription,
                ServiceCategoryCode = obj.ServiceCategoryCode
            };
        }

        internal static ServiceDTO FromModel(this ServiceModel obj)
        {
            if (obj == null)
            {
                return null;
            }
            return new ServiceDTO
            {
                Price = obj.Price,
                ServiceCode = obj.ServiceCode,
                ServiceDescription = obj.ServiceDescription,
                ServiceCategoryCode = obj.ServiceCategoryCode
            };
        }

        public static List<ServiceModel> ToModel(this ServiceDTO[] obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        public static ServiceDTO[] FromModel(this List<ServiceModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToArray();
        }

        #endregion

        #region PassengerService

        internal static PassengerServiceModel ToModel(this PassengerServiceDTO obj)
        {
            if (obj == null)
            {
                return null;
            }
            return new PassengerServiceModel
            {
                TicketId = obj.TicketId,
                ServiceCode = obj.ServiceCode
            };
        }

        internal static PassengerServiceDTO FromModel(this PassengerServiceModel obj)
        {
            if (obj == null)
            {
                return null;
            }
            return new PassengerServiceDTO
            {
                TicketId = obj.TicketId,
                ServiceCode = obj.ServiceCode
            };
        }

        public static List<PassengerServiceModel> ToModel(this PassengerServiceDTO[] obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        public static PassengerServiceDTO[] FromModel(this List<PassengerServiceModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToArray();
        }

        #endregion

        #region FlightModel
        public static FlightModel ToModel(this FlightDTO obj)
        {
            if (obj == null)
            {
                return null;
            }
            return new FlightModel
            {
                ArrivalAirport = obj.ArrivalAirport,
                ArrivalDateTime = obj.ArrivalDateTime,
                ArrivalTerminal = obj.ArrivalTerminal,
                DepartureAirport = obj.DepartureAirport,
                DepartureDateTime = obj.DepartureDateTime,
                DepartureTerminal = obj.DepartureTerminal,
                Distance = obj.Distance,
                FlightCode = obj.FlightCode,
                FlightId = obj.FlightId,
                FlightStatus = obj.FlightStatus,
                TaxSurcharge = obj.TaxSurcharge,
                AircraftId = obj.AircraftId
            };
        }

        public static List<FlightModel> ToModel(this List<FlightDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        public static FlightDTO FromModel(this FlightModel obj)
        {
            if (obj == null)
            {
                return null;
            }
            return new FlightDTO
            {
                ArrivalAirport = obj.ArrivalAirport,
                ArrivalDateTime = obj.ArrivalDateTime,
                ArrivalTerminal = obj.ArrivalTerminal,
                DepartureAirport = obj.DepartureAirport,
                DepartureDateTime = obj.DepartureDateTime,
                DepartureTerminal = obj.DepartureTerminal,
                Distance = obj.Distance,
                FlightCode = obj.FlightCode,
                FlightId = obj.FlightId,
                FlightStatus = obj.FlightStatus,
                TaxSurcharge = obj.TaxSurcharge,
                AircraftId = obj.AircraftId
            };
        }

        public static List<FlightDTO> FromModel(this List<FlightModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToList();
        }

        #endregion

        #region PaymentMethod

        public static PaymentMethodModel ToModel(this PaymentMethodDTO obj)
        {
            return new PaymentMethodModel
            {
                PaymentMethodCode = obj.PaymentMethodCode,
                PaymentMethodDescription = obj.PaymentMethodDescription
            };
        }

        public static List<PaymentMethodModel> ToModel(this List<PaymentMethodDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        #endregion

        #region CardType

        public static CardTypeModel ToModel(this CardTypeDTO obj)
        {
            return new CardTypeModel
            {
                CardTypeCode = obj.CardTypeCode,
                CardTypeDescription = obj.CardTypeDescription
            };
        }

        public static List<CardTypeModel> ToModel(this List<CardTypeDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        #endregion

        #region Card Payment

        public static CardPaymentDTO FromModel(this CardPaymentModel obj)
        {
            return new CardPaymentDTO
                       {
                           BookingId = obj.BookingId,
                           BillingAddress = new BillingAddressDTO
                                                {
                                                    AddressLine1 = obj.AddressLine1,
                                                    AddressLine2 = obj.AddressLine2,
                                                    AddressLine3 = obj.AddressLine3,
                                                    City = obj.City,
                                                    Country = obj.Country,
                                                    PostalCode = obj.PostalCode
                                                },
                           CardExpiryDate = obj.CardExpiryDate,
                           CardNo = obj.CardNo,
                           CardPaymentId = obj.CardPaymentId,
                           CardTypeCode = obj.CardTypeCode,
                           NameOnCard = obj.NameOnCard,
                           PaidAmount = obj.PaidAmount,
                           PaymentMethodCode = obj.PaymentMethodCode
                       };
        }

        #endregion

        #region Member Payment

        public static MemberPaymentDTO FromModel(this MemberPaymentModel obj)
        {
            return new MemberPaymentDTO
            {
                BookingId = obj.BookingId,
                PaymentPoints = obj.PaymentPoints
            };
        }

        #endregion

        #region Customer

        public static CustomerDTO FromModel(this CustomerModel obj)
        {
            if (obj.GetType() == typeof(MemberModel))
            {
                var member = obj as MemberModel;

                return new MemberDTO
                {
                    MemberId = member.MemberId,
                    Address1 = member.Address1,
                    Address2 = member.Address2,
                    Address3 = member.Address3,
                    City = member.City,
                    Country = member.Country,
                    CustomerId = member.CustomerId,
                    DateOfBirth = member.DateOfBirth,
                    FirstName = member.FirstName,
                    Gender = member.Gender,
                    LastName = member.LastName,
                    MemberPoints = member.MemberPoints,
                    PassportExpiryDate = member.PassportExpiryDate,
                    PassportNo = member.PassportNo,
                    Password = member.Password,
                    RoleId = 2,
                    UserName = member.UserName,
                    PostalCode = member.PostalCode//,
                    //Bookings = FromModel(member.Bookings)
                };
            }

            if (obj.GetType() == typeof(AgentModel))
            {
                var agent = obj as AgentModel;

                return new AgentDTO
                {
                    AgentId = agent.AgentId,
                    CompanyName = agent.CompanyName,
                    CustomerId = agent.CustomerId,
                    DateOfBirth = agent.DateOfBirth,
                    DiscountRate = agent.DiscountRate,
                    FirstName = agent.FirstName,
                    LastName = agent.LastName,
                    Gender = agent.Gender,
                    Password = agent.Password,
                    RoleId = 1,
                    UserName = agent.UserName
                };
            }

            return null;
        }

        public static List<CustomerDTO> FromModel(this List<CustomerModel> obj)
        {
            return obj == null ? null : obj.Select(FromModel).ToList();
        }

        public static CustomerModel ToModel(this CustomerDTO obj)
        {
            if (obj.GetType() == typeof(MemberDTO))
            {
                var member = obj as MemberDTO;

                return new MemberModel
                {
                    MemberId = member.MemberId,
                    Address1 = member.Address1,
                    Address2 = member.Address2,
                    Address3 = member.Address3,
                    City = member.City,
                    Country = member.Country,
                    CustomerId = member.CustomerId,
                    DateOfBirth = member.DateOfBirth,
                    FirstName = member.FirstName,
                    Gender = member.Gender,
                    LastName = member.LastName,
                    MemberPoints = member.MemberPoints,
                    PassportExpiryDate = member.PassportExpiryDate,
                    PassportNo = member.PassportNo,
                    Password = member.Password,
                    RoleId = 2,
                    UserName = member.UserName,
                    PostalCode = member.PostalCode//,
                    //Bookings = ToModel(member.Bookings)
                };
            }

            if (obj.GetType() == typeof(AgentDTO))
            {
                var agent = obj as AgentDTO;

                return new AgentModel
                {
                    AgentId = agent.AgentId,
                    CompanyName = agent.CompanyName,
                    CustomerId = agent.CustomerId,
                    DateOfBirth = agent.DateOfBirth,
                    DiscountRate = agent.DiscountRate,
                    FirstName = agent.FirstName,
                    LastName = agent.LastName,
                    Gender = agent.Gender,
                    Password = agent.Password,
                    RoleId = 1,
                    UserName = agent.UserName
                };
            }

            return null;
        }

        public static List<CustomerModel> ToModel(this List<CustomerDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        #endregion

        #region Title

        public static TitleModel ToModel(this TitleDTO obj)
        {
            return new TitleModel
            {
                titleCode = obj.TitleCode,
                titleDescription = obj.TitleDescription
            };
        }

        public static List<TitleModel> ToModel(this List<TitleDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }
        #endregion

        #region Airport

        public static AirportModel ToModel(this AirportDTO obj)
        {
            return new AirportModel
            {
                AirportCode = obj.AirportCode,
                AirportName = obj.AirportName,
                CityCode = obj.CityCode
            };
        }

        public static List<AirportModel> ToModel(this List<AirportDTO> obj)
        {
            return obj == null ? null : obj.Select(ToModel).ToList();
        }

        #endregion

    }
}